package com.zis.platform.service.impl;

import com.zis.base.impl.BaseInterImpl;
import com.zis.core.Constant;
import com.zis.core.ResponseEntity;
import com.zis.platform.model.Dictionary;
import com.zis.platform.model.DictionaryType;
import com.zis.platform.service.IDictTypeInter;
import org.beetl.sql.core.SQLManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Create by wangshen 2017/12/25
 */
@Service
@Transactional("txManager")
public class DictTypeInterImpl extends BaseInterImpl<DictionaryType> implements IDictTypeInter{

    @Autowired
    private SQLManager sqlManager;

    @Override
    public String insert(DictionaryType dictionaryType) throws Exception {
        dictionaryType.setCreateDate(new Date());
        int num = sqlManager.insert(dictionaryType);
        if(num > 0){
            return ResponseEntity.createNormalJsonResponse(Constant.DATA_RESULT_SUCCESS);
        }
        return ResponseEntity.createErrorJsonResponse(Constant.DATA_RESULT_ERROR);
    }

    @Override
    public String update(DictionaryType dictionaryType) throws Exception {
        dictionaryType.setUpdateDate(new Date());
        int num = sqlManager.updateTemplateById(dictionaryType);
        if(num > 0){
            return ResponseEntity.createNormalJsonResponse(Constant.DATA_RESULT_SUCCESS);
        }
        return ResponseEntity.createErrorJsonResponse(Constant.DATA_RESULT_ERROR);
    }

    @Override
    public String delete(String id) throws Exception {
        //判断是否存在下级
        DictionaryType dictType = selectById(id);
        Dictionary params = new Dictionary();
        params.setDictTypeCode(dictType.getDictTypeCode());
        List<Dictionary> dictList = sqlManager.template(params);
        if(dictList.size()>0){
            return ResponseEntity.createErrorJsonResponse(Constant.DATA_RESULT_SUB);
        }
        int num = sqlManager.deleteById(DictionaryType.class,id);
        if(num > 0){
            return ResponseEntity.createNormalJsonResponse(Constant.DATA_RESULT_SUCCESS);
        }
        return ResponseEntity.createErrorJsonResponse(Constant.DATA_RESULT_ERROR);
    }

    @Override
    public DictionaryType selectById(String id) throws Exception {
        return sqlManager.unique(DictionaryType.class, id);
    }

    @Override
    public List<DictionaryType> findByCondition(DictionaryType dictionaryType) throws Exception {
        return null;
    }
}
